package filter;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.TextUtils;

/**
 * 身份验证Filter Servlet Filter implementation class AuthorizationFilter
 */
// @WebFilter("/AuthorizationFilter")
public class AuthorizationFilter implements Filter {

	private String mUserName;
	private String mUserPwd;

	/**
	 * Default constructor.
	 */
	public AuthorizationFilter() {
		System.out.println("AuthorizationFilter constructor");
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		System.out.println("AuthorizationFilter init：" + fConfig.getFilterName());
		mUserName = fConfig.getInitParameter("user_name");
		mUserPwd = fConfig.getInitParameter("user_pwd");
		System.out.println("输出Init-UserParams：" + mUserName + "\tPwdParams：" + mUserPwd);
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		System.out.println("AuthorizationFilter doFilter");

		HttpServletRequest servletRequest = (HttpServletRequest) request;
		HttpServletResponse servletResponse = (HttpServletResponse) response;
		String userName = servletRequest.getParameter("user_name");
		String userPwd = servletRequest.getParameter("user_pwd");

		Enumeration<String> params = request.getParameterNames();
		while (params.hasMoreElements()) {
			String paramName = (String) params.nextElement();
			if (!TextUtils.isEmpty(paramName)) {
				System.out.println("输出请求的参数Key：" + paramName + "\tValue：" + servletRequest.getParameter(paramName));
			}
		}

		System.out.println("输出请求的Url：" + servletRequest.getRequestURL() + "\nUri：" + servletRequest.getRequestURI()
				+ "\n输出页面的请求方式：" + servletRequest.getMethod());

		if (servletRequest.getMethod().equals("POST")) {
			if (!TextUtils.isEmpty(userName) && !TextUtils.isEmpty(userPwd)) {
				if (mUserName.equals(userName) && mUserPwd.equals(userPwd)) {
					chain.doFilter(servletRequest, servletResponse);
				} else {
					System.out.println("账号密码错误");
					servletResponse.sendRedirect("/ServletApplication/common/Error.html");
					servletResponse.setStatus(200);
					servletResponse.getWriter().write("账号密码错误");
				}
			} else {
				System.out.println("账号密码不能为空");
				servletResponse.sendRedirect("/ServletApplication/Error.html");				
			}
		}else {
			chain.doFilter(servletRequest, servletResponse);
		}
		System.out.println("AuthorizationFilter doFilter---End");
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		System.out.println("AuthorizationFilter destroy");
	}

}
